#include <qobjectdefs.h>
#include <QString>

#include "../sql/QSqlQueryUtil.h"

class Worker : public QObject {
    Q_OBJECT
public slots:


    void doWork() {

        // 数据库插入操作
        QSqlQueryUtil dbUtil("gaomaolong", "task.db", "QSQLITE");

        QString createUserTable = R"(
        CREATE TABLE IF NOT EXISTS users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name VARCHAR(100) NOT NULL,
            age INTEGER
        )
    )";


        int result = dbUtil.executeUpdate(createUserTable);

        dbUtil.executeUpdate("delete from users");

        QString batchInsertUser = "INSERT INTO users (name, age) VALUES (?, ?)";
        QList<QVariantList> allParams;
        QVariantList nameParams;
        QVariantList agesParams;

        for (int i = 0; i < 100000; ++i) {
            nameParams << QString("批量用户%1").arg(i+1) ;  // 每一行的参数依次添加
            agesParams<< i+1;
            if (i%1000==0) {
                emit progress(i/1000);
                if (i%10000==0) {
                    allParams << nameParams << agesParams;
                    int num = dbUtil.batchInsert(batchInsertUser, allParams);
                    allParams.clear();
                    nameParams.clear();
                    agesParams.clear();
                }
            }
        }
        emit workFinished("Done");
    }
    signals:
        void workFinished(const QString &result);
        void progress(int size);
};
